package com.anXin.user.service.impl;

import com.anXin.apis.equipment.IEquipmentClient;
import com.anXin.gpt.BigModelNew;
import com.anXin.gpt.ChatSparkDesk;
import com.anXin.model.common.dtos.ResponseResult;
import com.anXin.model.common.enums.AppHttpCodeEnum;
import com.anXin.model.equipment.HeartRateZones;
import com.anXin.model.user.pojos.Addr;
import com.anXin.model.user.pojos.Oldman;
import com.anXin.model.user.pojos.Report;
import com.anXin.model.user.pojos.User;
import com.anXin.user.mapper.AddrMapper;
import com.anXin.user.mapper.ReportMapper;
import com.anXin.user.service.AddrService;
import com.anXin.user.service.OldmanService;
import com.anXin.user.service.ReportService;
import com.anXin.user.service.UserService;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import utils.thread.AppThreadLocalUtil;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.List;


@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements ReportService {

    private static final String date="today";
    @Resource
    private IEquipmentClient equipmentClient;
    @Resource
    private UserService userService;

    @Resource
    @Lazy
    private OldmanService oldmanService;

    @Override
    public ResponseResult add(Long id) throws Exception {
        Report report = new Report();
        report.setOldmanId(id);
//        ResponseResult rateZones = equipmentClient.getRateZones(date);
//        if (rateZones.getCode()==503)return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"获取心率数据失败");
//        LinkedHashMap<String, List<LinkedHashMap<String,Object>>> data = (LinkedHashMap<String, List<LinkedHashMap<String,Object>>>) rateZones.getData();
//        List<LinkedHashMap<String,Object>> heartRateZones = data.get("heartRateZones");
//        StringBuilder heartDescription=new StringBuilder("心率区间为：\n");
//        heartRateZones.forEach(heartRate -> {
//            if (heartRate.get("name").equals("Out of Range")){
//                heartDescription.append("在正常情况下，心率区间为："+heartRate.get("min")+"至"+heartRate.get("max")+"\n");
//            }else if (heartRate.get("name").equals("Fat Burn")){
//                heartDescription.append("在脂肪燃烧时，心率区间为："+heartRate.get("min")+"至"+heartRate.get("max")+"\n");
//            }else if (heartRate.get("name").equals("Cardio")){
//                heartDescription.append("在有氧运动时，心率区间为："+heartRate.get("min")+"至"+heartRate.get("max")+"\n");
//            }else if (heartRate.get("name").equals("Peak")){
//                heartDescription.append("峰值心率区间为："+heartRate.get("min")+"至"+heartRate.get("max")+"\n");
//            }
//        });
//        report.setHeartRateZones(heartDescription.toString());
//        ResponseResult bmi = equipmentClient.getBMI(date);
//        if (bmi.getCode()==503)return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"获取BMI数据失败");
//        String bmiDescription="BMI为："+ bmi.getData();
//        report.setBmi(bmiDescription);
//        System.out.println(bmiDescription);
//
//        ResponseResult weight = equipmentClient.getWeight(date);
//        if (weight.getCode()==503)return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"获取体重数据失败");
//        String weightDescription="体重为："+ weight.getData();
//        report.setWeight(weightDescription);
//        System.out.println(weightDescription);
//
//        ResponseResult steps = equipmentClient.getSteps(date);
//        if (steps.getCode()==503)return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"获取步数数据失败");
//        String stepsDescription="步数为："+ steps.getData();
//        report.setSteps(stepsDescription);
//        System.out.println(stepsDescription);
//
//        ResponseResult distance = equipmentClient.getDistance(date);
//        if (distance.getCode()==503)return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"获取运动距离数据失败");
//        String distanceDescription="运动距离为："+ distance.getData();
//        report.setDistance(distanceDescription);
//        System.out.println(distanceDescription);
//
//        ResponseResult elevation = equipmentClient.getElevation(date);
//        if (elevation.getCode()==503)return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"获取运动海拔数据失败");
//        String elevationDescription="运动海拔为："+ elevation.getData();
//        report.setElevation(elevationDescription);
//        System.out.println(elevationDescription);
//
//        ResponseResult floors = equipmentClient.getFloors(date);
//        if (floors.getCode()==503)return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"获取爬楼数据失败");
//        String floorsDescription="爬了："+ floors.getData()+"层楼";
//        report.setFloors(floorsDescription);
//        System.out.println(floorsDescription);
//
//        ResponseResult calories = equipmentClient.getCalories(date);
//        if (calories.getCode()==503)return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"获取卡路里数据失败");
//        String caloriesDescription="总消耗卡路里："+ calories.getData();
//        report.setCalories(caloriesDescription);
//        System.out.println(caloriesDescription);
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        String formattedDate = currentDate.format(formatter);
        User user = userService.query().eq("id", AppThreadLocalUtil.getUser().getId()).one();
        Oldman oldman = oldmanService.query().eq("id", id).one();
//        String question=heartDescription.toString()+bmiDescription+"\n"
//                +weightDescription+"\n"+stepsDescription+"\n"+distanceDescription+"\n"
//                +elevationDescription+"\n"+floorsDescription+"\n"+caloriesDescription+"\n";
//        String answer = ChatSparkDesk.gpt(question + "请你根据上述描述生成包含丰富建议的详细的健康报告");
        String answer = BigModelNew.Talk(  "请你为身体各项都正常的老人生成包含丰富建议的详细的健康报告");
//        report.setDescription("尊敬的"+user.getName()+"你好！\n这是您家人"+oldman.getName()+formattedDate+"的健康报告：\n"
//                +question+answer);
        report.setDescription("尊敬的"+user.getName()+"你好！\n这是您家人"+oldman.getName()+formattedDate+"的健康报告：\n"
               +answer);
        boolean save = save(report);
        if (save)return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public ResponseResult getList(Long id) {
        List<Report> list = query().eq("oldman_id", id).orderByDesc("created_time").list();
        if (list.isEmpty())return ResponseResult.errorResult(AppHttpCodeEnum.DATA_LIST_NULL);
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult Info(Long id) {
        Report report = query().eq("id", id).one();
        return ResponseResult.okResult(report);
    }
}